package me.zhengjie.controller.container;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.*;
import me.zhengjie.dao.dto.ContainerQueryCriteria;
import me.zhengjie.excel.DataHandle;
import me.zhengjie.server.ContainerService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("api/container")
public class ContainerController {

    private static final Logger inboundLog = LoggerFactory.getLogger("container");


    @Resource
    private ContainerService containerService;

    @RequestMapping("findContainerList")
    public Result<List<ContainerDao>> findStoreList(ContainerDao containerDao) {
        inboundLog.info("ContainerController findContainerList 查询载具信息");
        List<ContainerDao> containerList = containerService.findDataList(containerDao);
        return Result.success(containerList);
    }

    @RequestMapping("updateContainerById")
    public Result<?> updateContainerById(ContainerDao containerDao) {
        inboundLog.info("ContainerController updateStoreById 更新载具信息");
        containerService.updateDataById(containerDao);
        return Result.success();
    }

    @RequestMapping("importContainerExcel")
    public Result<?> importContainerExcel(String filePath) {
        inboundLog.info("ContainerController importContainerExcel 导入载具excel表");
        //String filePath = "D:\\载具信息.xlsx";
        List<ContainerDao> containerList = new ArrayList<>();
        DataHandle.HandleContainerData(filePath, containerList);
        containerService.saveBatch(containerList);
        return Result.success();
    }


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('container:list')")
    public void exportContainer(HttpServletResponse response, ContainerQueryCriteria criteria) throws IOException {
        containerService.download(containerService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询ContainerController")
    @PreAuthorize("@el.check('container:list')")
    public ResponseEntity<PageResult<ContainerDao>> queryContainer(ContainerQueryCriteria criteria) {
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(containerService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增ContainerController")
    @ApiOperation("新增ContainerController")
    @PreAuthorize("@el.check('container:add')")
    public ResponseEntity<Object> createContainer(@Validated @RequestBody ContainerDao resources) {
        containerService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改ContainerController")
    @ApiOperation("修改ContainerController")
    @PreAuthorize("@el.check('container:edit')")
    public ResponseEntity<Object> updateContainer(@Validated @RequestBody ContainerDao resources) {
        containerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除ContainerController")
    @ApiOperation("删除ContainerController")
    @PreAuthorize("@el.check('container:del')")
    public ResponseEntity<Object> deleteContainer(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        containerService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "disableVehicles", method = RequestMethod.PUT)
    public Result<?> disableVehicles(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        LambdaUpdateWrapper<ContainerDao> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ContainerDao::getId, ids);
        wrapper.set(ContainerDao::getDisable, 1);
        boolean update = containerService.update(wrapper);
        if (update){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @RequestMapping(value = "barCodeDamage", method = RequestMethod.PUT)
    public Result<?> barCodeDamage(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        LambdaUpdateWrapper<ContainerDao> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ContainerDao::getId, ids);
        wrapper.set(ContainerDao::getBarCodeBad, 1);
        boolean update = containerService.update(wrapper);
        if (update){
            return Result.success();
        }else {
            return Result.error();
        }

    }

    @RequestMapping(value = "containerCodeDamage", method = RequestMethod.PUT)
    public Result<?> containerCodeDamage(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        LambdaUpdateWrapper<ContainerDao> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ContainerDao::getId, ids);
        wrapper.set(ContainerDao::getContainerBad, 1);
        boolean update = containerService.update(wrapper);
        if (update){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @PostMapping(value = "handOutBound")
    @Log("手工出库页面")
    @ApiOperation("手工出库页面")
    public Result<PageResult<ContainerDao>> findContainer(@Validated @RequestBody ContainerParamDao containerDao) {inboundLog.info("ContainerController findContainerList 手工出库查询载具信息");
        PageResult<ContainerDao> newContainer = containerService.findNewContainer(containerDao);
        return Result.success(newContainer);
    }


    @PostMapping(value = "handOutBandDetail")
    @Log("手工出库详细页面")
    @ApiOperation("手工出库详细页面")
//    @PreAuthorize("@el.check('container:handOutBand')")
    public ResponseEntity<Object> handOutBandDetail(@Validated @RequestBody HandOutBandDao resources) {
        return new ResponseEntity<>(containerService.handOutBandDetail(resources),HttpStatus.OK);
    }

    @PostMapping(value = "handOutBand")
    @Log("手工出库")
    @ApiOperation("手工出库")
//    @PreAuthorize("@el.check('container:handOutBand')")
    public Result<Object> handOutBand(@Validated @RequestBody HandToOutBandDao param) throws IOException {
        return Result.success(containerService.handOutBand(param));
    }


    @PostMapping(value = "handScan")
    @Log("抽检扫码")
    @ApiOperation("抽检扫码")
//    @PreAuthorize("@el.check('container:handScan')")
    public Result<Object> handScan(@Validated @RequestBody HandSacnDao param) throws IOException {
        return Result.success(containerService.handScan(param));
    }

    @PostMapping(value = "handOut")
    @Log("手动解绑")
    @ApiOperation("手动解绑")
//    @PreAuthorize("@el.check('container:handScan')")
    public Result<Object> handOut(@Validated @RequestBody HandSacnDao param) throws IOException {
        return Result.success(containerService.handOut(param));
    }

}
